<?php
namespace Admin\Controller;

use Common\Common\CommonController;

/**
 * 优惠卷管理
 * @author yjh
 *
 */
class CouponController extends CommonController
{   
    private $Model;
    function __construct()
    {
        parent::__construct();
        
    }
    
    public function index(){
        if (!IS_AJAX) {
            $this->display();
        }
        
        $offset = I('get.offset', 0);
        $limit = I('get.limit');
        $where="";
        if($_GET['title']){
            $where = "title=".$_GET['title'];
        }
        $field = "id, title, total, quota, pv, uv, `status`, start_time, end_time, type, meet, value, value_random, member_level, single";
        
        $data = array('total' => 0, 'rows' => array());
        $data['total'] = M('mall_coupon')->field($field)->where($where)->limit($offset,$limit)->order("id asc")->count();
        if($data['total'] > 0){
            $data['rows'] = M('mall_coupon')->field($field)->where($where)->limit($offset,$limit)->order("id asc")->select();
        }
         
        $this->ajaxReturn($data);
    }
    
    /**
     * 添加
     */
    public function add(){
       
        $offset = I('get.offset', 0);
        $limit = I('get.limit');
        if (IS_AJAX) {
            $data = array('total' => 0, 'rows' => array());
            $data['total'] = M('mall_goods')->field('id,price,title,pic_url,stock')->limit($offset,$limit)->order("id asc")->count();
            if($data['total'] > 0){
                $data['rows'] = M('mall_goods')->field('id,price,title,pic_url,stock')->limit($offset,$limit)->order("id asc")->select();
            }
             
            $this->ajaxReturn($data);
        }
        if(IS_POST){
            $data = array(
                'title' => I('post.title'),
                'type' => I('post.type'),
                'quota' => I('post.quota'),
                'start_time' => strtotime(I('post.start_time')),
                'end_time' => strtotime(I('end_time')),
                'notice' => I('post.notice'),
                'meet' => I('post.meet'),
                'total' => I('post.total'),
                'range_type' => I('post.range_type'),
            );
            
             //面值判断
            if($_POST['sjz']==''){
                $data['value'] = $_POST['value'];
            }else{
                $data['value'] = mt_rand($_POST['value'], $_POST['sjz']);
            }
            
            //标题验证
            if(empty($data['title'])){
                $this->error("请输入标题");
            }
         
            
            M('mall_coupon')->data($data)->add();
            $this->success("添加成功","/coupon/");
        }
     $this->display();
    }
    
    /**
     * 编辑
     */
    public function edit($id = 0){
    if(IS_POST){
            $data = I("post.");
            M('mall_coupon')->save($data);
            $this->success("编辑成功");
        }
        
        $data = M('mall_coupon')->find($id);
        if(empty($data)){
            $this->error('数据不存在或已被删除！');
        }
        
        $this->assign(array(
            'data' => $data
        ));
        $this->display();
    }
    
    public function delete($id = 0){
        if (empty($id)) {
            $this->error('删除项不能为空！');
        }
        $result = M('mall_coupon')->delete($id);
        if ($result) {
            $this->success('删除成功！');
        }
        $this->error('删除失败！');
        
    }
    
    /**
     * 指定商品
     */
    
    public function specified(){
        var_dump($_POST);exit;
    }
   
    
    
   
    
    
    
    
    
    
    
    
    
    
}
?>
